#!/bin/bash
set -eu
scriptdir=$(cd $(dirname $0) && pwd)

if ! type aws > /dev/null; then
    echo "Install the AWS CLI before running this script." >&2
    exit 1
fi

hedy_env=$1
day=$2

cache_root=~/.cache/hedy-logs/${hedy_env}
cache_dir=${cache_root}/${day}
mkdir -p "$cache_dir"

export AWS_DEFAULT_REGION=eu-central-1
export AWS_PROFILE=hedy-logs-viewer
bucket=hedy-query-logs

aws s3 sync s3://${bucket}/${hedy_env}/${day} $cache_dir

# Need to make sure every file ends in a newline
finalfile=$cache_root/$day.jsonl
sed -e '$a\' $cache_dir/* > $finalfile

echo "Now run a command like:"
echo ""
echo "Page loads that take a long time:"
echo "   cat $finalfile | recs grep '{{duration_ms}} > 1000' | tee _lastquery.jsonl | recs totable -k start_time,duration_ms,method,path,'!_ms\$!' | less -S"
echo ""
echo "Count of failures:"
echo "   cat $finalfile | recs grep '{{http_code}} ne 200' | tee _lastquery.jsonl | recs collate -k http_code,method,path -a count | recs sort -k count=-n | recs totable | less -S"
